package com.lottery.im.module.setting.ui;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.lottery.im.MyApplication;
import com.lottery.im.R;
import com.lottery.im.api.Api;
import com.lottery.im.api.result.entity.UserInfo;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.util.StatusBarHelper;
import com.lottery.im.util.transformations.GlideCircleBorderTransform;
import com.uuzuche.lib_zxing.activity.CodeUtils;

/*
 * 我的二维码、群聊二维码
 * */
public class MyQrCodeFragment extends BaseSwipeFragment {

  private final static String NAME_KEY = "NAME_KEY";
  private final static String AVATARURL_KEY = "AVATARURL_KEY";
  private final static String QRCONTENT_KEY = "QRCONTENT_KEY";

  public static MyQrCodeFragment create(String name, String avatarUrl,
      String qrContent) {
    MyQrCodeFragment fragment = new MyQrCodeFragment();
    Bundle b = new Bundle();
    b.putString(NAME_KEY, name);
    b.putString(AVATARURL_KEY, avatarUrl);
    b.putString(QRCONTENT_KEY, qrContent);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.iv_portrait) ImageView mIvPortrait;
  @BindView(R.id.tv_name) TextView mTvName;
  @BindView(R.id.tv_tip) TextView mTvTip;
  @BindView(R.id.iv_qr_code) ImageView mIvQrCode;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_my_qrcode;
  }

  @Override protected void initPresenter() {
    initView();
  }

  private void initView() {
    StatusBarHelper.setStatusBarDarkMode(getActivity());

    Bundle arguments = getArguments();
    if (arguments != null) {
      mTvTitle.setText("群聊二维码");
      mTvTip.setText("扫一扫加，入群聊");
      Glide.with(this)
          .load(arguments.getString(AVATARURL_KEY))
          .apply(new RequestOptions()
              .bitmapTransform(new GlideCircleBorderTransform(
                  mActivity.getResources().getDimension(R.dimen.portrait_border_width),
                  ContextCompat.getColor(mActivity, R.color.white)))
              .placeholder(R.drawable.icon_conversation_de)
              .error(R.drawable.icon_conversation_de))
          .into(mIvPortrait);
      mTvName.setText(arguments.getString(NAME_KEY));
      Bitmap bitmap = CodeUtils.createImage(arguments.getString(QRCONTENT_KEY), 143, 143, null);
      mIvQrCode.setImageBitmap(bitmap);
    } else {
      mTvTitle.setText(R.string.qr_code);
      UserInfo userInfo = MyApplication.getInstance().getUserInfo();
      Glide.with(this)
          .load(Api.BASEADDRESS + userInfo.getUserPic())
          .apply(new RequestOptions()
              .bitmapTransform(new GlideCircleBorderTransform(
                  mActivity.getResources().getDimension(R.dimen.portrait_border_width),
                  ContextCompat.getColor(mActivity, R.color.white)))
              .placeholder(R.drawable.icon_conversation_de)
              .error(R.drawable.icon_conversation_de))
          .into(mIvPortrait);
      mTvName.setText(userInfo.getUserName());
      Bitmap bitmap = CodeUtils.createImage(userInfo.getThirdAccId(), 143, 143, null);
      mIvQrCode.setImageBitmap(bitmap);
    }
  }

  @OnClick({ R.id.iv_back, R.id.btn_share }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.btn_share:
        break;
    }
  }
}
